package com.sdk.es.http.util;

import com.sdk.es.sdk.entity.common.EsPageResult;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class SearchHitsCustomUtil {

    public static <T> EsPageResult<T> toPageEsResult(SearchHits<T> searchHits) {
        return EsPageResult.of(toList(searchHits), searchHits.getTotalHits());
    }

    public static <T> List<T> toList(SearchHits<T> searchHits) {
        return searchHits.hasSearchHits() ? searchHits.get().map(SearchHit::getContent).collect(Collectors.toList())
                : Collections.emptyList();
    }

}
